/*
 * jPhoneMini.java
 *
 * Created on Mar 22, 2010, 6:31:03 PM
 *
 * @author pquiring@gmail.com
 *
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

import javaforce.*;
import javaforce.voip.*;

/** jphonelite Panel */

public class PhoneMini extends BasePhone implements MeterController, GUI {

  public String number = "";  //forced number to always dial

  /** Creates new form jPhoneMini */
  public PhoneMini(WindowController wc, String number) {
    try {
      this.number = number;
      initBasePhone(this, wc);
      Settings.isApplet = true;
      initComponents();
      init();
      //must set focus to something or VK_BACK_SPACE beeps
      SwingUtilities.invokeLater(new Runnable(){
        public void run(){
          call.requestFocusInWindow();  //must be called from EDT or when window is visible
        }
      });
    } catch (Exception e) {
      JFLog.log("Error:" + e);
    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        shareDesktopMenu = new javax.swing.JPopupMenu();
        shareDesktopViewOnly = new javax.swing.JMenuItem();
        shareDesktopRemoteControl = new javax.swing.JMenuItem();
        call = new javax.swing.JToggleButton();
        end = new javax.swing.JToggleButton();
        n1 = new javax.swing.JButton();
        n2 = new javax.swing.JButton();
        n3 = new javax.swing.JButton();
        n4 = new javax.swing.JButton();
        n5 = new javax.swing.JButton();
        n6 = new javax.swing.JButton();
        n7 = new javax.swing.JButton();
        n8 = new javax.swing.JButton();
        n9 = new javax.swing.JButton();
        n0 = new javax.swing.JButton();
        nstar = new javax.swing.JButton();
        npound = new javax.swing.JButton();
        clear = new javax.swing.JButton();
        status = new javax.swing.JTextField();
        meterRec = new javax.swing.JProgressBar();
        meterPlay = new javax.swing.JProgressBar();
        mute = new javax.swing.JButton();
        headsetPic = new javax.swing.JLabel();
        micPic = new javax.swing.JLabel();
        backSpace = new javax.swing.JButton();
        micScale = new javax.swing.JLabel();
        spkScale = new javax.swing.JLabel();
        volRec = new javax.swing.JSlider();
        volPlay = new javax.swing.JSlider();
        spk = new javax.swing.JButton();
        labels1 = new javax.swing.JLabel();
        spkPic = new javax.swing.JLabel();
        mutePic = new javax.swing.JLabel();
        labels2 = new javax.swing.JLabel();

        shareDesktopViewOnly.setText("Share Desktop (view only)");
        shareDesktopMenu.add(shareDesktopViewOnly);

        shareDesktopRemoteControl.setText("Share Desktop (remote control)");
        shareDesktopMenu.add(shareDesktopRemoteControl);

        setLayout(null);

        call.setText("Call");
        call.setFocusCycleRoot(true);
        call.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                callActionPerformed(evt);
            }
        });
        add(call);
        call.setBounds(10, 40, 70, 50);

        end.setText("End");
        end.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                endActionPerformed(evt);
            }
        });
        add(end);
        end.setBounds(210, 40, 70, 50);

        n1.setText("1");
        n1.setMargin(new java.awt.Insets(2, 2, 2, 2));
        n1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                n1ActionPerformed(evt);
            }
        });
        add(n1);
        n1.setBounds(90, 40, 30, 30);

        n2.setText("2");
        n2.setMargin(new java.awt.Insets(2, 2, 2, 2));
        n2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                n2ActionPerformed(evt);
            }
        });
        add(n2);
        n2.setBounds(130, 40, 30, 30);

        n3.setText("3");
        n3.setMargin(new java.awt.Insets(2, 2, 2, 2));
        n3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                n3ActionPerformed(evt);
            }
        });
        add(n3);
        n3.setBounds(170, 40, 30, 30);

        n4.setText("4");
        n4.setMargin(new java.awt.Insets(2, 2, 2, 2));
        n4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                n4ActionPerformed(evt);
            }
        });
        add(n4);
        n4.setBounds(90, 80, 30, 30);

        n5.setText("5");
        n5.setMargin(new java.awt.Insets(2, 2, 2, 2));
        n5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                n5ActionPerformed(evt);
            }
        });
        add(n5);
        n5.setBounds(130, 80, 30, 30);

        n6.setText("6");
        n6.setMargin(new java.awt.Insets(2, 2, 2, 2));
        n6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                n6ActionPerformed(evt);
            }
        });
        add(n6);
        n6.setBounds(170, 80, 30, 30);

        n7.setText("7");
        n7.setMargin(new java.awt.Insets(2, 2, 2, 2));
        n7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                n7ActionPerformed(evt);
            }
        });
        add(n7);
        n7.setBounds(90, 120, 30, 30);

        n8.setText("8");
        n8.setMargin(new java.awt.Insets(2, 2, 2, 2));
        n8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                n8ActionPerformed(evt);
            }
        });
        add(n8);
        n8.setBounds(130, 120, 30, 30);

        n9.setText("9");
        n9.setMargin(new java.awt.Insets(2, 2, 2, 2));
        n9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                n9ActionPerformed(evt);
            }
        });
        add(n9);
        n9.setBounds(170, 120, 30, 30);

        n0.setText("0");
        n0.setMargin(new java.awt.Insets(2, 2, 2, 2));
        n0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                n0ActionPerformed(evt);
            }
        });
        add(n0);
        n0.setBounds(130, 160, 30, 30);

        nstar.setText("*");
        nstar.setMargin(new java.awt.Insets(2, 2, 2, 2));
        nstar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nstarActionPerformed(evt);
            }
        });
        add(nstar);
        nstar.setBounds(90, 160, 30, 30);

        npound.setText("#");
        npound.setMargin(new java.awt.Insets(2, 2, 2, 2));
        npound.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                npoundActionPerformed(evt);
            }
        });
        add(npound);
        npound.setBounds(170, 160, 30, 30);

        clear.setText("CLR");
        clear.setToolTipText("Clear");
        clear.setMargin(new java.awt.Insets(2, 0, 2, 0));
        clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearActionPerformed(evt);
            }
        });
        add(clear);
        clear.setBounds(150, 200, 30, 30);

        status.setEditable(false);
        add(status);
        status.setBounds(10, 10, 270, 20);

        meterRec.setOrientation(1);
        add(meterRec);
        meterRec.setBounds(30, 100, 10, 90);

        meterPlay.setOrientation(1);
        add(meterPlay);
        meterPlay.setBounds(270, 100, 10, 90);

        mute.setToolTipText("Mute");
        mute.setMargin(new java.awt.Insets(2, 0, 2, 0));
        mute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                muteActionPerformed(evt);
            }
        });
        add(mute);
        mute.setBounds(10, 200, 30, 30);

        headsetPic.setText(".");
        add(headsetPic);
        headsetPic.setBounds(210, 120, 30, 30);

        micPic.setText(".");
        add(micPic);
        micPic.setBounds(50, 120, 30, 30);

        backSpace.setText("<");
        backSpace.setMargin(new java.awt.Insets(2, 2, 2, 2));
        backSpace.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backSpaceActionPerformed(evt);
            }
        });
        add(backSpace);
        backSpace.setBounds(110, 200, 30, 30);

        micScale.setText(".");
        add(micScale);
        micScale.setBounds(20, 100, 10, 90);

        spkScale.setText(".");
        add(spkScale);
        spkScale.setBounds(260, 100, 10, 90);

        volRec.setOrientation(javax.swing.JSlider.VERTICAL);
        volRec.setValue(75);
        volRec.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                volRecStateChanged(evt);
            }
        });
        add(volRec);
        volRec.setBounds(0, 100, 20, 90);

        volPlay.setOrientation(javax.swing.JSlider.VERTICAL);
        volPlay.setValue(75);
        volPlay.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                volPlayStateChanged(evt);
            }
        });
        add(volPlay);
        volPlay.setBounds(240, 100, 20, 90);

        spk.setMargin(new java.awt.Insets(2, 0, 2, 0));
        spk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spkActionPerformed(evt);
            }
        });
        add(spk);
        spk.setBounds(250, 200, 30, 30);
        add(labels1);
        labels1.setBounds(10, 70, 270, 10);
        add(spkPic);
        spkPic.setBounds(250, 230, 30, 10);
        add(mutePic);
        mutePic.setBounds(10, 230, 30, 10);
        add(labels2);
        labels2.setBounds(10, 110, 270, 10);
    }// </editor-fold>//GEN-END:initComponents

    private void n3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_n3ActionPerformed
      addDigit('3');
    }//GEN-LAST:event_n3ActionPerformed

    private void n1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_n1ActionPerformed
      addDigit('1');
    }//GEN-LAST:event_n1ActionPerformed

    private void n2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_n2ActionPerformed
      addDigit('2');
    }//GEN-LAST:event_n2ActionPerformed

    private void n4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_n4ActionPerformed
      addDigit('4');
    }//GEN-LAST:event_n4ActionPerformed

    private void n5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_n5ActionPerformed
      addDigit('5');
    }//GEN-LAST:event_n5ActionPerformed

    private void n6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_n6ActionPerformed
      addDigit('6');
    }//GEN-LAST:event_n6ActionPerformed

    private void n7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_n7ActionPerformed
      addDigit('7');
    }//GEN-LAST:event_n7ActionPerformed

    private void n8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_n8ActionPerformed
      addDigit('8');
    }//GEN-LAST:event_n8ActionPerformed

    private void n9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_n9ActionPerformed
      addDigit('9');
    }//GEN-LAST:event_n9ActionPerformed

    private void nstarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nstarActionPerformed
      addDigit('*');
    }//GEN-LAST:event_nstarActionPerformed

    private void n0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_n0ActionPerformed
      addDigit('0');
    }//GEN-LAST:event_n0ActionPerformed

    private void npoundActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_npoundActionPerformed
      addDigit('#');
    }//GEN-LAST:event_npoundActionPerformed

    private void clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearActionPerformed
      clear();
    }//GEN-LAST:event_clearActionPerformed

    private void callActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_callActionPerformed
      selectLine(0);
      lines[0].dial = number;
      call();
    }//GEN-LAST:event_callActionPerformed

    private void endActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_endActionPerformed
      end();
    }//GEN-LAST:event_endActionPerformed

    private void muteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_muteActionPerformed
      toggleMute();
    }//GEN-LAST:event_muteActionPerformed

    private void backSpaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backSpaceActionPerformed
      addDigit((char)KeyEvent.VK_BACK_SPACE);
    }//GEN-LAST:event_backSpaceActionPerformed

    private void volRecStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_volRecStateChanged
      setVolRec();
    }//GEN-LAST:event_volRecStateChanged

    private void volPlayStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_volPlayStateChanged
      setVolPlay();
    }//GEN-LAST:event_volPlayStateChanged

    private void spkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_spkActionPerformed
      toggleSpeaker();
    }//GEN-LAST:event_spkActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backSpace;
    private javax.swing.JToggleButton call;
    private javax.swing.JButton clear;
    private javax.swing.JToggleButton end;
    private javax.swing.JLabel headsetPic;
    private javax.swing.JLabel labels1;
    private javax.swing.JLabel labels2;
    private javax.swing.JProgressBar meterPlay;
    private javax.swing.JProgressBar meterRec;
    private javax.swing.JLabel micPic;
    private javax.swing.JLabel micScale;
    private javax.swing.JButton mute;
    private javax.swing.JLabel mutePic;
    private javax.swing.JButton n0;
    private javax.swing.JButton n1;
    private javax.swing.JButton n2;
    private javax.swing.JButton n3;
    private javax.swing.JButton n4;
    private javax.swing.JButton n5;
    private javax.swing.JButton n6;
    private javax.swing.JButton n7;
    private javax.swing.JButton n8;
    private javax.swing.JButton n9;
    private javax.swing.JButton npound;
    private javax.swing.JButton nstar;
    private javax.swing.JPopupMenu shareDesktopMenu;
    private javax.swing.JMenuItem shareDesktopRemoteControl;
    private javax.swing.JMenuItem shareDesktopViewOnly;
    private javax.swing.JButton spk;
    private javax.swing.JLabel spkPic;
    private javax.swing.JLabel spkScale;
    private javax.swing.JTextField status;
    private javax.swing.JSlider volPlay;
    private javax.swing.JSlider volRec;
    // End of variables declaration//GEN-END:variables

  /** Init panel variables. */

  public void init() {
    loadIcons();
    Settings.loadSettings();
    setFonts();
    wc.setPanelAlwaysOnTop(Settings.current.alwaysOnTop);
    JFLog.setEnabled(!Settings.current.disableLogging);
    JFLog.log("jphonelite/" + version);

    loadingConfig = true;
    updateContactList();
    loadingConfig = false;
    updateRecentList();
    lines = new PhoneLine[6];
    for(int a=0;a<6;a++) {
      lines[a] = new PhoneLine();
    }
    numButtons = new JButton[12];
    numButtons[0] = n1;
    numButtons[1] = n2;
    numButtons[2] = n3;
    numButtons[3] = n4;
    numButtons[4] = n5;
    numButtons[5] = n6;
    numButtons[6] = n7;
    numButtons[7] = n8;
    numButtons[8] = n9;
    numButtons[9] = n0;
    numButtons[10] = nstar;
    numButtons[11] = npound;
    for(int a=0;a<12;a++) {
      numButtons[a].addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
          JButton o = (JButton)e.getSource();
          pressDigit(o.getText().charAt(0));
        }
        public void mouseReleased(MouseEvent e) {
          JButton o = (JButton)e.getSource();
          releaseDigit(o.getText().charAt(0));
        }
      });
    }

    //set PICs

    call.setText("");
    call.setIcon(ii[PIC_CALL]);
//    call.setPressedIcon(ii[PIC_CALL2]);
    call.setSelectedIcon(ii[PIC_CALL2]);

    end.setText("");
    end.setIcon(ii[PIC_END]);
//    end.setPressedIcon(ii[PIC_END2]);
    end.setSelectedIcon(ii[PIC_END2]);

    micPic.setText("");
    micPic.setIcon(ii[PIC_MIC]);
    headsetPic.setText("");
    headsetPic.setIcon(ii[PIC_HEADSET]);

    spk.setText("");
    spk.setIcon(ii[Settings.current.speakerMode ? PIC_GREEN : PIC_GREY]);
    spkPic.setText("");
    spkPic.setIcon(ii[PIC_SPK]);

    mute.setText("");
    mute.setIcon(ii[PIC_GREY]);
    mutePic.setText("");
    mutePic.setIcon(ii[PIC_MUTE]);

    keepAliveinit();
    boolean sound_init = sound.init(lines, this);
    setScales();  //requires sound.init()
    new Thread() {
      public void run() {
        reRegisterAll();
      }
    }.start();
    if (!sound_init) {
      JOptionPane.showMessageDialog(this,
        "No compatible sound found",
        "Warning",
        JOptionPane.INFORMATION_MESSAGE);
    }
  }

  /** MeterController : Sets the recording level on the display meter. */

  public void setMeterRec(int lvl) {meterRec.setValue(lvl);}

  /** MeterController : Sets the playback level on the display meter. */

  public void setMeterPlay(int lvl) {meterPlay.setValue(lvl);}

  /** MeterController : Sets the speaker mic status (green=true red=false). */

  public void setSpeakerStatus(boolean state) {
    if (!Settings.current.speakerMode) return;  //could happen just after turned off
    spk.setIcon(ii[state ? PIC_GREEN : PIC_RED]);
  }

  /** Change volume from slider control. */

  public void setVolRec() {
    sound.setVolRec(volRec.getValue());
  }

  /** Change volume from slider control. */

  public void setVolPlay() {
    sound.setVolPlay(volPlay.getValue());
  }

  public void setScales() {
    if (sound.isSWVolRec()) {
      micScale.setText("");
      micScale.setIcon(ii[PIC_SWSCALE]);
      volRec.setValue(75);
      sound.setVolRec(75);
    } else {
      micScale.setText("");
      micScale.setIcon(ii[PIC_HWSCALE]);
      volRec.setValue(100);
      sound.setVolRec(100);
    }
    if (sound.isSWVolPlay()) {
      spkScale.setText("");
      spkScale.setIcon(ii[PIC_SWSCALE]);
      volPlay.setValue(75);
      sound.setVolPlay(75);
    } else {
      spkScale.setText("");
      spkScale.setIcon(ii[PIC_HWSCALE]);
      volPlay.setValue(100);
      sound.setVolPlay(100);
    }
  }

  /** Select a new line. */

  public void selectLine(int newline) {
    //make sure line is valid
    if ((line != -1) && (lines[line].dtmf != 'x')) lines[line].dtmfend = true;
    line = newline;
    if (line == -1) {
      status.setText("");
      return;
    }
    sound.selectLine(line);
    updateLine();
  }

  /** Update the number to be dialed, status and buttons for current selected line. */

  public void updateLine() {
    end.setSelected(false);
    if (lines[line].incall) {
      call.setSelected(true);
    } else {
      call.setSelected(false);
    }
    status.setText(lines[line].status);
  }

  public void updateCallButton(boolean state) {
    call.setSelected(state);
  }

  public void updateEndButton(boolean state) {
    end.setSelected(state);
  }

  public void endLineUpdate(int xline) {
  }

  public void callInviteUpdate() {
  }

  /** Sets a contacts icon status when received from a NOTIFY message. */

  public void setStatus(String number, String server, String status) {}

  /** Updates contact list after a change. */

  public void updateContactList() {}

  /** Update recent lists after a change. */

  public void updateRecentList() {}

  public void hld_setIcon(ImageIcon ii) {
  }

  public void aa_setIcon(ImageIcon ii) {
  }

  public void ac_setIcon(ImageIcon ii) {
  }

  public void dnd_setIcon(ImageIcon ii) {
  }

  public void cnf_setIcon(ImageIcon ii) {
  }

  public void mute_setIcon(ImageIcon ii) {
    mute.setIcon(ii);
  }

  public void spk_setIcon(ImageIcon ii) {
    spk.setIcon(ii);
  }

  public void setFonts() {
//    JFLog.log("Font name= " + l1.getFont().getName() + ",stype=" + l1.getFont().getStyle() + ",size=" + l1.getFont().getSize() );
    //sets font sizes for all buttons
    int small = 10;
    int large = 12;  //default size
    if (Settings.current.smallerFont) {
      small -= 2;
      large -= 2;
    }
    Font smallFont = new java.awt.Font("Dialog", 0, small);
    Font largeFont = new java.awt.Font("Dialog", 0, large);

    //these components use three or more letter and require a smaller font
    mute.setFont(smallFont);
    clear.setFont(smallFont);
  }

  public void onRegister(SIPClient sip) {  }

  public void rtp_jpeg_receive(byte data[], int pos, int len) {}

  public String getLineStatus() {
    return status.getText();
  }

}
